<?php
//$pdo = null;
/**
* zeus_open_session()
*
* @return boolean
*/
function zeus_open_session()
{
    global $pdo;
    if (!isset($pdo)) {
        $dsn = zeus_dsn();
        try {
            $pdo = new PDO($dsn, DB_USER, DB_PASSWORD);
        }
        catch(PDOException $e) {
            // TO DO:
            // save error to database
        }
        return true;
    } else {
        zeus_do_nothing();
    }
}
/**
* zeus_close_session()
*
* @return
*/
function zeus_close_session()
{
    global $pdo;
    if (isset($pdo)) {
        unset($pdo);
    } else {
        zeus_do_nothing();
    }
    return true;
}
/**
* zeus_read_session()
*
* @param string $sid
* @return
*/
function zeus_read_session($sid = '')
{
    global $pdo;
    if (is_object($pdo)) {
        zeus_do_nothing();
    } else {
        zeus_open_session();
    }
    $sql = 'SELECT session_data,last_accessed FROM ' . DB_PREFIX . 'session WHERE session_id =\'' . $sid . '\';';
    $result = $pdo->query($sql);
    if (!empty($result)) {
        return $result;
    } else {
        return null;
    }
}
/**
* zeus_write_session()
*
* @param string $sid
* @param string $data
* @return boolean
*/
function zeus_write_session($sid = '', $data = '')
{
    global $pdo;
    if (is_object($pdo)) {
        zeus_do_nothing();
    } else {
        zeus_open_session();
    }
    $sql = 'REPLACE INTO ' . DB_PREFIX . 'session(session_id,session_data) VALUES (\'' . $sid . '\',\'' . $data . '\');';
    $count = $pdo->exec($sql);
    if (0 == $count) {
        return false;
    } else {
        return $count;
    }
}
/**
* zeus_destory_session()
*
* @param string $sid
* @return boolean
*/
function zeus_destory_session($sid = '')
{
    global $pdo;
    if (is_object($pdo)) {
        zeus_do_nothing();
    } else {
        zeus_open_session();
    }
    $sql = 'DELETE FROM ' . DB_PREFIX . 'session WHERE session_id =\'' . $sid . '\';';
    $count = $pdo->exec($sql);
    $_SESSION = array();
    if (0 == $count) {
        return false;
    } else {
        return $count;
    }
}
/**
* zeus_clean_session()
*
* @param mixed $expire
* @return
*/
function zeus_clean_session($expire)
{
    global $pdo;
    if (is_object($pdo)) {
        zeus_do_nothing();
    } else {
        zeus_open_session();
    }
    $sql = 'DELETE FROM ' . DB_PREFIX . 'session WHERE last_accessed <  NOW()-\'' . $expire . '\';';
    $count = $pdo->exec($sql);
    $_SESSION = array();
    if (0 == $count) {
        return false;
    } else {
        return $count;
    }
}
// Delcare the functions to use
/* session_set_save_handler('zeus_open_session',
    'zeus_close_session' ,
    'zeus_read_session',
    'zeus_write_session',
    'zeus_destory_session' ,
    'zeus_clean_session');
*/
// Start the session
session_start();
?>